package com.example.administrator.testproject.download;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.administrator.testproject.R;
import com.example.administrator.testproject.download.adapter.RecyclerProgressAdapter;
import com.example.administrator.testproject.download.bean.LiteOrmDownInfo;
import com.example.administrator.testproject.download.util.DBUtils;
import com.example.administrator.testproject.download.util.LiteDbDownUtil;
import com.example.administrator.testproject.download.view.RoundProgressBar;

import java.io.File;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class RecyclerActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerProgressAdapter recyclerAdapter;

    List<LiteOrmDownInfo> listData;
    LiteDbDownUtil dbUtil;
    private RoundProgressBar projressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        projressBar = (RoundProgressBar)findViewById(R.id.pb_download_icon);
        projressBar.setMax(113);
        projressBar.setStatus(RoundProgressBar.STATUS.DOWN);
        new Timer().schedule(new TimerTask() {
            int progress=0;
            @Override
            public void run() {
//                projressBar.setMax(11307617);
                progress+=5;
//                Log.e("Tag","progress"+progress);
//                projressBar.setProgress(progress);
            }
        }, 1200, 600);


        dbUtil=LiteDbDownUtil.getInstance();
        listData=DBUtils.queryDownAll();
        if (listData!=null){
            for (LiteOrmDownInfo info:listData){
                dbUtil.deleteDowninfo(info);
            }
        }
        /*第一次模拟服务器返回数据掺入到数据库中*/
        if(listData.isEmpty()){
            String[] downUrl=new String[]{
                    "http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4",
                    "http://video.jiecao.fm/5/1/%E8%87%AA%E5%8F%96%E5%85%B6%E8%BE%B1.mp4",
            "http://img.jiaoyuchuang.com/20171219/636cc4e6-ca3c-405d-93a7-3d10d58bf8a0.mp4"};
            for (int i = 0; i < downUrl.length; i++) {
                File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                        "test1"+i + ".mp4");
                LiteOrmDownInfo apkApi=new LiteOrmDownInfo(downUrl[i]);
//                apkApi.id==i;
                apkApi.setState(DownState.START);
                apkApi.setSavePath(outputFile.getAbsolutePath());
                dbUtil.save(apkApi);
            }
            listData=dbUtil.queryDownAll();
        }
        recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        recyclerAdapter = new RecyclerProgressAdapter(this,listData);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(recyclerAdapter);
    }
}
